<?php
namespace Home\Model;

use Think\Model;

class DataModel extends Model
{

    private $flag = false;

    /**
     * *插入一条兼职信息**
     */
    public function insertMessage($data, $extData)
    {
        $data['add_time'] = $data['update_time'] = time();
        $this->startTrans();
        if ($this->add($data) == false)
            $this->flag = true;
        $extData['did'] = $this->getLastInsID();
        if (M('DataMessage')->add($extData) == false)
            $this->flag = true;
        if ($this->flag == true) {
            $this->rollback();
            return false;
        }

        return $this->commit();
    }


    public function insertJianLi($data, $extData)
    {
        $data['add_time'] = $data['update_time'] = time();
        $this->startTrans();
        if ($this->add($data) == false)
            $this->flag = true;
        $extData['did'] = $this->getLastInsID();
        if (M('DataResume')->add($extData) == false)
            $this->flag = true;
        if ($this->flag == true) {
            $this->rollback();
            return false;
        }
        return $this->commit();
    }

    public function getJianli($where = array("1=1"), $limit = 10, $order = "add_time desc")
    {
        $datas = $this->where($where)->join(' JOIN __DATA_RESUME__ ON __DATA__.id = __DATA_RESUME__.did')->limit($limit)->order($order)->select();
        return $datas;
    }


    public function getMessage($where = array("1=1"), $limit = 10, $order = "add_time desc")
    {
        $datas = $this->where($where)->limit($limit)->order($order)->select();
        //
        if (empty($datas)) return false;
        $cid = arrayFileds($datas, "cate");
        $cates = D("Category")->categoryInfo($cid);
        if ($cates) {
            $cates = array2Map($cates, "id");
        }

        foreach ($datas as $k => &$v) {
            $v['cate_name'] = isset($cates[$v['cate']]) ? $cates[$v['cate']]['title'] : "";

        }
        return $datas;
    }

    public function getMsgByChid($cid, $limit = "limit 0,10", $order = "add_time desc")
    {
        $sql = " select a.*,b.* from data as a ,data_message as b where b.channel = {$cid} and b.did=a.id  order by $order   $limit ";
        $datas = $this->query($sql);
        return $datas;
    }

    /**
     * 获取一条完整的数据
     * @param unknown $mId
     */
    public function oneMessage($mId)
    {
        $base_info = $this->where(array('id' => $mId))->find();
        if (empty($base_info)) return false;
        if ($base_info['type'] == 0) {
            $ext_info = M('dataMessage')->where(array('did' => $mId))->find();
        } elseif ($base_info['type'] == 1) {
            $ext_info = M('dataResume')->where(array('did' => $mId))->find();
        }
        if (empty($ext_info)) return $base_info;
        $base_info['user_info'] = D("User")->getUser($base_info['author']);
        $ext_info['place'] = D('Region')->getMessagePlace(array($ext_info['province'], $ext_info['city'], $ext_info['country']));


        $arr = array_merge($base_info, $ext_info);
        unset($base_info);
        unset($ext_info);
        $arr['channel_name'] = D("Category")->where(array('id' => $arr['channel']))->getField("title");

        return $arr;
    }


}